#include "widget.h"
#include "ui_widget.h"
#include"QString"
#include"greedy.h"
#include"dichotomy.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
this->setWindowTitle("青蛙跳石头");
this->setWindowIcon(QIcon(":/QWT.jpeg"));

}

Widget::~Widget()
{
    delete ui;
}

vector<int> Widget::extractIntegers(const QString &str)
{
    vector<int> integers;
    QRegExp regex("\\d+"); // 匹配一个或多个数字
    int pos = 0;
    while ((pos = regex.indexIn(str, pos)) != -1) {
        // 使用QString::mid()提取匹配的子串
        QString match = str.mid(pos, regex.matchedLength());
        // 将QString转换为int并添加到列表中
        bool ok;
        int num = match.toInt(&ok);
        if (ok) {
            integers.push_back(num);
        }
        // 移动到下一个可能的匹配位置
        pos += regex.matchedLength();
    }
    return integers;
}


void Widget::on_pushButton_tuichu_clicked()
{
    this->close();
}

void Widget::on_pushButton_erfen_clicked()
{
    QString shuzu = ui->textEdit_shuzu->toPlainText();
    if(shuzu == "") return;
    vector<int> integers = extractIntegers(shuzu);
    int num = Greedy().maxJump(integers);
    ui->lineEdit->setText(QString::number(num));
}

void Widget::on_pushButton_tanxin_clicked()
{
    QString shuzu = ui->textEdit_shuzu->toPlainText();
    if(shuzu == "") return;
    vector<int> integers = extractIntegers(shuzu);
    int num = dichotomy().maxJump(integers);
    ui->lineEdit->setText(QString::number(num));
}

void Widget::on_pushButton_qingkong_clicked()
{
    ui->textEdit_shuzu->setText("");
    ui->lineEdit->setText("");
}
